#include "StdAfx.h"
#include "ControlUI.h"
#include "PaintManagerUI.h"

CControlUI::CControlUI(void):
m_pManager(NULL),
m_pParent(NULL),
m_pTag(NULL),
m_chShortcut('\0'),
m_bVisible(true),
m_bFocused(false),
m_bEnabled(true)
{
	::ZeroMemory(&m_rcItem,sizeof(RECT));
}

CControlUI::~CControlUI(void)
{
	if(m_pManager != NULL)
		m_pManager->ReapObjects(this);
}

bool CControlUI::IsVisible() const
{
	return m_bVisible;
}

bool CControlUI::IsEnabled() const
{
	return m_bEnabled;
}

bool CControlUI::IsFocused() const
{
	return m_bFocused;
}

UINT CControlUI::GetControlFlags() const
{
	return 0;
}

void CControlUI::SetVisible(bool bVisible)
{
	if(m_bVisible == bVisible)
		return;
	m_bVisible = bVisible;
	if(m_pManager != NULL)
		m_pManager->UpdateLayout();
}

void CControlUI::SetEnabled(bool bEnabled)
{
	m_bEnabled = bEnabled;
	Invalidate();
}

bool CControlUI::Activate()
{
	if(!IsVisible())
		return false;
	if(!IsEnabled())
		return false;
	return true;
}

CControlUI* CControlUI::GetParent() const
{
	return m_pParent;
}

void CControlUI::SetFocus()
{
	if(m_pManager != NULL)
		m_pManager->SetFocus(this);
}

void CControlUI::SetShortcut(TCHAR ch)
{
	m_chShortcut = ch;
}

TCHAR CControlUI::GetShortcut() const
{
	return m_chShortcut;
}

CStdString CControlUI::GetText() const
{
	return m_sText;
}

void CControlUI::SetText(LPCTSTR pstrText)
{
	m_sText = pstrText;
	Invalidate();
}

UINT_PTR CControlUI::GetTag() const
{
	return m_pTag;
}

void CControlUI::SetTag(UINT_PTR pTag)
{
	m_pTag = pTag;
}

void CControlUI::SetToolTip(LPCTSTR pstrText)
{
	m_sToolTip = pstrText;
}

CStdString CControlUI::GetToolTip() const
{
	return m_sToolTip;
}

void CControlUI::Init()
{
}

CPaintManagerUI* CControlUI::GetManager() const
{
	return m_pManager;
}

void CControlUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent)
{
	bool bInit = (m_pManager == NULL);
	m_pManager = pManager;
	m_pParent = pParent;
	if(bInit)
		Init();
}

CStdString CControlUI::GetName() const
{
	return m_sName;
}

LPVOID CControlUI::GetInterface(LPCTSTR pstrName)
{
	if(_tcscmp(pstrName,_T("Control")) == 0)
		return this;
	return NULL;
}

CControlUI* CControlUI::FindControl(FINDCONTROLPROC Proc, LPVOID pData, UINT uFlags)
{
	if((uFlags & UIFIND_VISIBLE) != 0 && !IsVisible())
		return NULL;
	if((uFlags & UIFIND_ENABLED) != 0 && !IsEnabled())
		return NULL;
	if((uFlags & UIFIND_HITTEST) != 0 && !::PtInRect(&m_rcItem, * static_cast<LPPOINT>(pData)))
		return NULL;
	return Proc(this,pData);
}

RECT CControlUI::GetPos() const
{
	return m_rcItem;
}

void CControlUI::SetPos(RECT rc)
{
	m_rcItem = rc;
	Invalidate();
}

void CControlUI::Invalidate()
{
	if(m_pManager != NULL)
		m_pManager->Invalidate(m_rcItem);
}

void CControlUI::UpdateLayout()
{
	if(m_pManager != NULL)
		m_pManager->UpdateLayout();
}

void CControlUI::Event(TEventUI &event)
{
	if(event.Type == UIEVENT_SETCURSOR)
	{
		::SetCursor(::LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));
		return ;
	}
	if(event.Type == UIEVENT_SETFOCUS)
	{
		m_bFocused = true;
		Invalidate();
		return ;
	}
	if(event.Type == UIEVENT_KILLFOCUS)
	{
		m_bFocused = false;
		Invalidate();
		return ;
	}
	if(event.Type == UIEVENT_TIMER)
	{
		m_pManager->SendNotify(this,_T("timer"),event.wParam,event.lParam);
		return;
	}
	if(m_pParent != NULL)
		m_pParent->Event(event);
}

void CControlUI::Notify(TNotifyUI &msg)
{
}

void CControlUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if(_tcscmp(pstrName,_T("pos")) == 0)
	{
		RECT rcPos = {0};
		LPTSTR pstr = NULL;
		rcPos.left = _tcstol(pstrValue,&pstr,10);
		ASSERT(pstr);
		rcPos.top = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		rcPos.right = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		rcPos.bottom = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		SetPos(rcPos);
	}
	else if(_tcscmp(pstrName,_T("name")) == 0)
		SetName(pstrValue);
	else if(_tcscmp(pstrName,_T("text")) == 0)
		SetText(pstrValue);
	else if(_tcscmp(pstrName,_T("tooltip")) == 0)
		SetToolTip(pstrValue);
	else if(_tcscmp(pstrName,_T("enabled")) == 0)
		SetEnabled(_tcscmp(pstrValue,_T("true")) == 0);
	else if(_tcscmp(pstrName,_T("visible")) == 0)
		SetVisible(_tcscmp(pstrValue,_T("true")) == 0);
	else if(_tcscmp(pstrName,_T("shortcut")) == 0)
		SetShortcut(pstrValue[0]);

}

CControlUI* CControlUI::ApplyAttributeList(LPCTSTR pstrList)
{
	CStdString sItem;
	CStdString sValue;
	while(*pstrList != '\0')
	{
		sItem.Empty();
		sValue.Empty();
		while(*pstrList != '\0' && *pstrList != '=')
			sItem += *pstrList++;
		ASSERT(*pstrList == '=');
		if(*pstrList++ != '=')
			return this;
		ASSERT(*pstrList == '\"');
		if(*pstrList++ == '\"')
			return this;
		while(*pstrList != '\0' && *pstrList != '\"')
			sValue += *pstrList++;
		ASSERT(*pstrList == '\"');
		if(*pstrList++ != '\"')
			return this;
		SetAttribute(sItem,sValue);
		if(*pstrList++ != ',')
			return this;
	}
	return this;
}

void CControlUI::SetName(LPCTSTR pstrName)
{
	m_sName = pstrName;
}